home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / tools / czesc_2 / ispell-3.3ljr / ispell / minrexx.c < prev    next >
C/C++ Source or Header  |  1992-09-22  |  15KB  |  454 lines

  1. /*
  2.  *   This is an example of how REXX messages might be handled.  This is
  3.  *   a `minimum' example that both accepts asynchronous REXX messages and
  4.  *   can request REXX service.
  5.  *
  6.  *   Read this entire file!  It's short enough.
  7.  *
  8.  *   It is written in such a fashion that it can be attached to a program
  9.  *   with a minimum of fuss.  The only external symbols it makes available
  10.  *   are the seven functions and RexxSysBase.
  11.  *
  12.  *   This code is by Radical Eye Software, but it is put in the public
  13.  *   domain.  I would appreciate it if the following string was left in
  14.  *   both as a version check and as thanks from you for the use of this
  15.  *   code.
  16.  *
  17.  *   If you modify this file for your own use, don't bump the version
  18.  *   number; add a suffix, such as 1.0a or 1.0.3 or something, so we
  19.  *   don't have fake `versions' floating around.
  20.  */
  21. static char *blurb = "Radical Eye MinRexx 0.4ljr";
  22.  
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include "ispell.h"
  26.  
  27. /*
  28.  *   All of our local globals, hidden from sight.
  29.  */
  30. static struct MsgPort *rexxPort;/* this is *our* rexx port */
  31. static int bringerdown;        /* are we trying to shut down? */
  32. static struct rexxCommandList *globalrcl;    /* our command association list */
  33. static long stillNeedReplies;    /* how many replies are pending? */
  34. static long rexxPortBit;    /* what bit to wait on for Rexx? */
  35. static char *extension;        /* the extension for macros */
  36. static void (*userdisp) (struct RexxMsg *, struct rexxCommandList *, char *);    /* the user's dispatch function */
  37. static struct RexxMsg *oRexxMsg;/* the outstanding Rexx message */
  38. /*
  39.  *   Our library base.  Don't you dare close this!
  40.  */
  41. struct RxsLib *RexxSysBase;
  42. /*
  43.  *   This is the main entry point into this code.
  44.  */
  45. long upRexxPort (char *s, struct rexxCommandList *rcl, char *exten, void(*uf)(struct RexxMsg *, struct rexxCommandList *, char *))
  46. /*
  47.  *   The first argument is the name of your port to be registered;
  48.  *   this will be used, for instance, with the `address' command of ARexx.
  49.  *   The second argument is an association list of command-name/user-data
  50.  *   pairs.  It's an array of struct rexxCommandList, terminated by a
  51.  *   structure with a NULL in the name field. The commands are case
  52.  *   sensitive.  The user-data field can contain anything appropriate,
  53.  *   perhaps a function to call or some other data.
  54.  *   The third argument is the file extension for ARexx macros invoked
  55.  *   by this program.  If you supply this argument, any `primitive' not
  56.  *   in the association list rcl will be sent out to ARexx for
  57.  *   interpretation, thus allowing macro programs to work just like
  58.  *   primitives.  If you do not want this behavior, supply a `NULL'
  59.  *   here, and those commands not understood will be replied with an
  60.  *   error value of RXERRORNOCMD.
  61.  *   The fourth argument is the user dispatch function.  This function
  62.  *   will *only* be called from rexxDisp(), either from the user calling
  63.  *   this function directly, or from dnRexxPort().  Anytime a command
  64.  *   match is found in the association list, this user-supplied function
  65.  *   will be called with two arguments---the Rexx message that was
  66.  *   received, and a pointer to the association pair.  This function
  67.  *   should return a `1' if the message was replied to by the function
  68.  *   and a `0' if the default success code of (0, 0) should be returned.
  69.  *   Note that the user function should never ReplyMsg() the message;
  70.  *   instead he should indicate the return values with replyRexxCmd();
  71.  *   otherwise we lose track of the messages that still lack replies.
  72.  *   upRexxPort() returns the signal bit to wait on for Rexx messages.
  73.  *   If something goes wrong, it simply returns a `0'.  Note that this
  74.  *   function is safe to call multiple times because we check to make
  75.  *   sure we haven't opened already.  It's also a quick way to change
  76.  *   the association list or dispatch function.
  77.  */
  78. {
  79.  
  80. /*
  81.  *   Some basic error checking.
  82.  */
  83.   if (rcl == NULL || uf == NULL)
  84.     return (0L);
  85. /*
  86.  *   If we aren't open, we make sure no one else has opened a port with
  87.  *   this name already.  If that works, and the createport succeeds, we
  88.  *   fill rexxPortBit with the value to return.
  89.  *
  90.  *   Note that rexxPortBit will be 0 iff rexxPort is NULL, so the check
  91.  *   for rexxPort == NULL also insures that our rexxPortBit is 0.
  92.  */
  93.   if (rexxPort == NULL)
  94.     {
  95.       Forbid ();
  96.       if (FindPort (s) == NULL)
  97.     rexxPort = CreateMsgPort ();
  98.       rexxPort->mp_Node.ln_Name = s;
  99.       rexxPort->mp_Node.ln_Pri = 0;
  100.       AddPort (rexxPort);
  101.       Permit ();
  102.       if (rexxPort != NULL)
  103.     rexxPortBit = 1L << rexxPort->mp_SigBit;
  104.     }
  105. /*
  106.  *   Squirrel away these values for our own internal access, and return
  107.  *   the wait bit.
  108.  */
  109.   globalrcl = rcl;
  110.   extension = exten;
  111.   userdisp = uf;
  112.   return (rexxPortBit);
  113. }
  114.  
  115. /*
  116.  *   This function closes the rexx library, but only if it is open
  117.  *   and we aren't expecting further replies from REXX.  It's
  118.  *   *private*, but it doesn't have to be; it's pretty safe to
  119.  *   call anytime.
  120.  */
  121. void closeRexxLib (void)
  122. {
  123.   if (stillNeedReplies == 0 && RexxSysBase)
  124.     {
  125.       CloseLibrary ((struct Library *)RexxSysBase);
  126.       RexxSysBase = NULL;
  127.     }
  128. }
  129.  
  130. /*
  131.  *   This function closes down the Rexx port.  It is always safe to
  132.  *   call, and should *definitely* be made a part of your cleanup
  133.  *   routine.  No arguments and no return.  It removes the Rexx port,
  134.  *   replies to all of the messages and insures that we get replies
  135.  *   to all the ones we sent out, closes the Rexx library, deletes the
  136.  *   port, clears a few flags, and leaves.
  137.  */
  138. void dnRexxPort (void)
  139. {
  140.   if (rexxPort)
  141.     {
  142.       RemPort (rexxPort);
  143.       bringerdown = 1;
  144. /*
  145.  *   A message still hanging around?  We kill it off.
  146.  */
  147.       if (oRexxMsg)
  148.     {
  149.       oRexxMsg->rm_Result1 = RXERRORIMGONE;
  150.       ReplyMsg ((struct Message *)oRexxMsg);
  151.       oRexxMsg = NULL;
  152.     }
  153.       while (stillNeedReplies)
  154.     {
  155.       WaitPort (rexxPort);
  156.       dispRexxPort ();
  157.     }
  158.       closeRexxLib ();
  159.       DeleteMsgPort (rexxPort);
  160.       rexxPort = NULL;
  161.     }
  162.   rexxPortBit = 0;
  163. }
  164.  
  165. /*
  166.  *   Here we dispatch any REXX messages that might be outstanding.
  167.  *   This is the main routine for handling Rexx messages.
  168.  *   This function is fast if no messages are outstanding, so it's
  169.  *   pretty safe to call fairly often.
  170.  *
  171.  *   If we are bring the system down and flushing messages, we reply
  172.  *   with a pretty serious return code RXERRORIMGONE.
  173.  *
  174.  *   No arguments, no returns.
  175.  */
  176. void dispRexxPort (void)
  177. {
  178.   register struct RexxMsg *RexxMsg;
  179.   register struct rexxCommandList *rcl;
  180.   register char *p;
  181.   register int dontreply;
  182.  
  183. /*
  184.  *   If there's no rexx port, we're out of here.
  185.  */
  186.   if (rexxPort == NULL)
  187.     return;
  188. /*
  189.  *   Otherwise we have our normal loop on messages.
  190.  */
  191.   while (RexxMsg = (struct RexxMsg *) GetMsg (rexxPort))
  192.     {
  193. /*
  194.  *   If we have a reply to a message we sent, we look at the second
  195.  *   argument.  If it's set, it's a function we are supposed to call
  196.  *   so we call it.  Then, we kill the argstring and the message
  197.  *   itself, decrement the outstanding count, and attempt to close
  198.  *   down the Rexx library.  Note that this call only succeeds if
  199.  *   there are no outstanding messages.  Also, it's pretty quick, so
  200.  *   don't talk to me about efficiency.
  201.  */
  202.       if (RexxMsg->rm_Node.mn_Node.ln_Type == NT_REPLYMSG)
  203.     {
  204.       if (RexxMsg->rm_Args[1])
  205.         {
  206.           (*((int (*) (struct RexxMsg *)) (RexxMsg->rm_Args[1]))) (RexxMsg);
  207.         }
  208.       DeleteArgstring (RexxMsg->rm_Args[0]);
  209.       DeleteRexxMsg (RexxMsg);
  210.       stillNeedReplies--;
  211.       closeRexxLib ();
  212. /*
  213.  *   The default case is we got a message and we need to check it for
  214.  *   primitives.  We skip past any initial tabs or spaces and initialize
  215.  *   the return code fields.
  216.  */
  217.     }
  218.       else
  219.     {
  220.       p = (char *) RexxMsg->rm_Args[0];
  221.       while (*p > 0 && *p <= ' ')
  222.         p++;
  223.       RexxMsg->rm_Result1 = 0;
  224.       RexxMsg->rm_Result2 = 0;
  225. /*
  226.  *   If somehow the reply is already done or postponed, `dontreply' is
  227.  *   set.
  228.  */
  229.       dontreply = 0;
  230. /*
  231.  *   If the sky is falling, we just blow up and replymsg.
  232.  */
  233.       if (bringerdown)
  234.         {
  235.           RexxMsg->rm_Result1 = RXERRORIMGONE;
  236. /*
  237.  *   Otherwise we cdr down our association list, comparing commands,
  238.  *   until we get a match.  If we get a match, we call the dispatch
  239.  *   function with the appropriate arguments, and break out.
  240.  */
  241.         }
  242.       else
  243.         {
  244.           oRexxMsg = RexxMsg;
  245.           for (rcl = globalrcl; rcl->name; rcl++)
  246.         {
  247.           if (cmdcmp (rcl->name, p) == 0)
  248.             {
  249.               if (p[strlen (rcl->name)])
  250.             (*userdisp) (RexxMsg, rcl, p + strlen (rcl->name) + 1);
  251.               else
  252.             (*userdisp) (RexxMsg, rcl, p + strlen (rcl->name));
  253.               break;
  254.             }
  255.         }
  256. /*
  257.  *   If we broke out, rcl will point to the command we executed; if we
  258.  *   are at the end of the list, we didn't understand the command.  In
  259.  *   this case, if we were supplied an extension in upRexxPort, we know
  260.  *   that we should send the command out, so we do so, synchronously.
  261.  *   The synchronous send takes care of our reply.  If we were given a
  262.  *   NULL extension, we bitch that the command didn't make sense to us.
  263.  */
  264.           if (rcl->name == NULL)
  265.         {
  266.           if (extension)
  267.             {
  268.               syncRexxCmd (RexxMsg->rm_Args[0], RexxMsg);
  269.               dontreply = 1;
  270.             }
  271.           else
  272.             {
  273.               RexxMsg->rm_Result1 = RXERRORNOCMD;
  274.             }
  275.         }
  276.         }
  277. /*
  278.  *   Finally, reply if appropriate.
  279.  */
  280.       oRexxMsg = NULL;
  281.       if (!dontreply)
  282.         ReplyMsg ((struct Message *)RexxMsg);
  283.     }
  284.     }
  285. }
  286.  
  287. /*
  288.  *   This is the function we use to see if the command matches
  289.  *   the command string.  Not case sensitive.  Make sure all commands
  290.  *   are given in lower case!
  291.  */
  292. int cmdcmp (char *c, char *m)
  293. {
  294.   while (*c && ((*c == *m) || (*c == *m + 32 && ('a' <= *c && *c <= 'z'))))
  295.     {
  296.       c++;
  297.       m++;
  298.     }
  299.   if (!(*c) && *m)
  300.     return ((int) *m != ' ');
  301.   return ((int) *c);
  302. }
  303.  
  304. /*
  305.  *   Opens the Rexx library if unopened.  Returns success (1) or
  306.  *   failure (0).  This is another function that is *private* but
  307.  *   that doesn't have to be.
  308.  */
  309. int openRexxLib (void)
  310. {
  311.   if (RexxSysBase)
  312.     return (1);
  313.   return ((RexxSysBase = (struct RxsLib *)OpenLibrary (RXSNAME, 0L)) != NULL);
  314. }
  315.  
  316. /*
  317.  *   This is the general ARexx command interface, but is not the one
  318.  *   you will use most of the time; ones defined later are easier to
  319.  *   understand and use.  But they all go through here.
  320.  */
  321. struct RexxMsg *sendRexxCmd (char *s, void (*f)(struct RexxMsg *), STRPTR p1, STRPTR p2, STRPTR p3)
  322. /*
  323.  *   The first parameter is the command to send to Rexx.
  324.  *   The second parameter is either NULL, indicating that the command
  325.  *   should execute asynchronously, or a function to be called when the
  326.  *   message we build up and send out here finally returns.  Please note
  327.  *   that the function supplied here could be called during cleanup after
  328.  *   a fatal error, so make sure it is `safe'.  This function always is
  329.  *   passed one argument, the RexxMsg that is being replied.
  330.  *   These are up to three arguments to be stuffed into the RexxMsg we
  331.  *   are building up, making the values available when the message is
  332.  *   finally replied to.  The values are stuffed into Args[2]..Args[4].
  333.  */
  334. {
  335.   register struct MsgPort *rexxport;
  336.   register struct RexxMsg *RexxMsg;
  337.  
  338. /*
  339.  *   If we have too many replies out there, we just return failure.
  340.  *   Note that you should check the return code to make sure your
  341.  *   message got out!  Then, we forbid, and make sure that:
  342.  *      - we have a rexx port open
  343.  *      - Rexx is out there
  344.  *      - the library is open
  345.  *      - we can create a message
  346.  *      - we can create an argstring
  347.  *
  348.  *   If all of these succeed, we stuff a few values and send the
  349.  *   message, permit, and return.
  350.  */
  351.   if (rexxPort == NULL || stillNeedReplies > MAXRXOUTSTANDING - 1)
  352.     return (NULL);
  353.   RexxMsg = NULL;
  354.   if (openRexxLib () && (RexxMsg =
  355.       CreateRexxMsg (rexxPort, extension, rexxPort->mp_Node.ln_Name)) &&
  356.       (RexxMsg->rm_Args[0] = CreateArgstring (s, (long) strlen (s))))
  357.     {
  358.       RexxMsg->rm_Action = RXCOMM;
  359.       RexxMsg->rm_Args[1] = (STRPTR) f;
  360.       RexxMsg->rm_Args[2] = p1;
  361.       RexxMsg->rm_Args[3] = p2;
  362.       RexxMsg->rm_Args[4] = p3;
  363.       RexxMsg->rm_Node.mn_Node.ln_Name = RXSDIR;
  364.       Forbid ();
  365.       if (rexxport = FindPort (RXSDIR))
  366.     PutMsg (rexxport, (struct Message *)RexxMsg);
  367.       Permit ();
  368.       if (rexxport)
  369.     {
  370.       stillNeedReplies++;
  371.       return (RexxMsg);
  372.     }
  373.       else
  374.     DeleteArgstring (RexxMsg->rm_Args[0]);
  375.     }
  376.   if (RexxMsg)
  377.     DeleteRexxMsg (RexxMsg);
  378.   closeRexxLib ();
  379.   return (NULL);
  380. }
  381.  
  382. /*
  383.  *   This function is used to send out an ARexx message and return
  384.  *   immediately.  Its single parameter is the command to send.
  385.  */
  386. struct RexxMsg *asyncRexxCmd (char *s)
  387. {
  388.   return (sendRexxCmd (s, NULL, NULL, NULL, NULL));
  389. }
  390.  
  391. /*
  392.  *   This function sets things up to reply to the message that caused
  393.  *   it when we get a reply to the message we are sending out here.
  394.  *   But first the function we pass in, which actually handles the reply.
  395.  *   Note how we get the message from the Args[2]; Args[0] is the command,
  396.  *   Args[1] is this function, and Args[2]..Args[4] are any parameters
  397.  *   passed to sendRexxCmd() as p1..p3.  We pass the result codes right
  398.  *   along.
  399.  */
  400. void replytoit (struct RexxMsg *msg)
  401. {
  402.   register struct RexxMsg *omsg;
  403.  
  404.   omsg = (struct RexxMsg *) (msg->rm_Args[2]);
  405.   replyRexxCmd (omsg, msg->rm_Result1, msg->rm_Result2, NULL);
  406.   ReplyMsg ((struct Message *)omsg);
  407. }
  408.  
  409. /*
  410.  *   This function makes use of everything we've put together so far,
  411.  *   and functions as a synchronous Rexx call; as soon as the macro
  412.  *   invoked here returns, we reply to `msg', passing the return codes
  413.  *   back.
  414.  */
  415. struct RexxMsg *syncRexxCmd (char *s, struct RexxMsg *msg)
  416. {
  417.   return (sendRexxCmd (s, &replytoit, (STRPTR)msg, NULL, NULL));
  418. }
  419.  
  420. /*
  421.  *   There are times when you want to pass back return codes or a
  422.  *   return string; call this function when you want to do that,
  423.  *   and return `1' from the user dispatch function so the main
  424.  *   event loop doesn't reply (because we reply here.)  This function
  425.  *   always returns 1.
  426.  */
  427. void replyRexxCmd (struct RexxMsg *msg, long primary, long secondary, char *string)
  428. /*
  429.  *   The first parameter is the message we are replying to.
  430.  *   The next two parameters are the primary and secondary return
  431.  *   codes.
  432.  *   The final parameter is a return string.  This string is only
  433.  *   returned if the primary return code is 0, and a string was
  434.  *   requested.
  435.  *
  436.  *   We also note that we have replied to the message that came in.
  437.  */
  438. {
  439. /*
  440.  *   Note how we make sure the Rexx Library is open before calling
  441.  *   CreateArgstring . . . and we close it down at the end, if possible.
  442.  */
  443.   if (primary == 0 && (msg->rm_Action & (1L << RXFB_RESULT)))
  444.     {
  445.       if (string && openRexxLib ())
  446.     secondary = (long) CreateArgstring (string, (long) strlen (string));
  447.       else
  448.     secondary = 0L;
  449.     }
  450.   msg->rm_Result1 = primary;
  451.   msg->rm_Result2 = secondary;
  452.   closeRexxLib ();
  453. }
  454.